package co.chuangdian.oflauncher.widgets.system;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

public class MusicList {

	public static MusicList list_object;
	
	private Context mContext;
	
	public static MusicList getInstance(Context context) {
		if(list_object == null) {
			list_object = new MusicList(context);
		}
		return list_object;
	}
	
	MusicList(Context context) {
		mContext = context;
	}
	
	public List<Music> getMusicList() {
		List<Music> list = new ArrayList<Music>();
		
		ContentResolver cr = mContext.getContentResolver();
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
	        	    null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		
		if (cursor.moveToFirst()) {
			do {
				 Music music = new Music();
				 String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
				 String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
				 int album_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
				 String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
				 int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				 int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
				 if (isMusic == 0) {
					 return null;					 
				 }
				 music.setUrl(url);
				 music.setTitle(title);
				 music.setAlbumId(album_id);
                 music.setSinger(singer);
                 music.setDuration(duration);
                 list.add(music);
			} while (cursor.moveToNext());
		}
		
		return list;
	}
}
